﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Data;

namespace Clipper.DAL
{

    /// <summary>
    /// 数据访问类:StandardState
    /// </summary>
    public partial class StandardState
    {
        public StandardState()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return SQLiteHelper.GetMaxID("StateID", "StandardState");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int StateID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from StandardState");
            strSql.Append(" where StateID=@StateID ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@StateID", DbType.Int16,1)			};
            parameters[0].Value = StateID;

            return SQLiteHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.StandardState model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into StandardState(");
            strSql.Append("StateID,Name)");
            strSql.Append(" values (");
            strSql.Append("@StateID,@Name)");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@StateID", DbType.Int16,1),
					new SQLiteParameter("@Name", DbType.String)};
            parameters[0].Value = model.StateID;
            parameters[1].Value = model.Name;

            int rows = SQLiteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.StandardState model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update StandardState set ");
            strSql.Append("Name=@Name");
            strSql.Append(" where StateID=@StateID ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@Name", DbType.String),
					new SQLiteParameter("@StateID", DbType.Int16,1)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.StateID;

            int rows = SQLiteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int StateID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from StandardState ");
            strSql.Append(" where StateID=@StateID ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@StateID", DbType.Int16,1)			};
            parameters[0].Value = StateID;

            int rows = SQLiteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string StateIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from StandardState ");
            strSql.Append(" where StateID in (" + StateIDlist + ")  ");
            int rows = SQLiteHelper.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.StandardState GetModel(int StateID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select StateID,Name from StandardState ");
            strSql.Append(" where StateID=@StateID ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@StateID", DbType.Int16,1)			};
            parameters[0].Value = StateID;

            Model.StandardState model = new Model.StandardState();
            DataSet ds = SQLiteHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.StandardState DataRowToModel(DataRow row)
        {
            Model.StandardState model = new Model.StandardState();
            if (row != null)
            {
                if (row["StateID"] != null && row["StateID"].ToString() != "")
                {
                    model.StateID = int.Parse(row["StateID"].ToString());
                }
                if (row["Name"] != null)
                {
                    model.Name = row["Name"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select StateID,Name ");
            strSql.Append(" FROM StandardState ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLiteHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM StandardState ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = SQLiteHelper.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.StateID desc");
            }
            strSql.Append(")AS Row, T.*  from StandardState T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return SQLiteHelper.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@tblName", DbType.VarChar, 255),
                    new SQLiteParameter("@fldName", DbType.VarChar, 255),
                    new SQLiteParameter("@PageSize", DbType.Int32),
                    new SQLiteParameter("@PageIndex", DbType.Int32),
                    new SQLiteParameter("@IsReCount", DbType.bit),
                    new SQLiteParameter("@OrderType", DbType.bit),
                    new SQLiteParameter("@strWhere", DbType.VarChar,1000),
                    };
            parameters[0].Value = "StandardState";
            parameters[1].Value = "StateID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return SQLiteHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
